package cn.easybuy.web.pre;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.easybuy.entity.Product;
import cn.easybuy.param.ProductParam;
import cn.easybuy.service.proudct.ProductCategoryService;
import cn.easybuy.service.proudct.ProductCategoryServiceImpl;
import cn.easybuy.service.proudct.ProductService;
import cn.easybuy.service.proudct.ProductServiceImpl;
import cn.easybuy.utils.EmptyUtils;
import cn.easybuy.utils.Pager;
import cn.easybuy.utils.ProductCategoryVo;
import cn.easybuy.web.AbstractServlet;


@WebServlet(urlPatterns = {"/Product"},name = "Product")
public class ProductServlet extends AbstractServlet {

	private ProductService productService;
	private ProductCategoryService productCategoryService; 

	public String queryProductList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		String categoryId = request.getParameter("categoryId");
		String keyWord = request.getParameter("keyWord");
		//第几页
		String currentPageStr = request.getParameter("currentPageStr");
		//页面的大小
		String pageSizeStr = request.getParameter("pageSize");
		int rowPerPage = EmptyUtils.isNotEmpty(pageSizeStr) ? Integer.parseInt(pageSizeStr) : 8;
		int currentPage = EmptyUtils.isNotEmpty(currentPageStr) ? Integer.parseInt(currentPageStr) : 1; 
		
		ProductParam params = new ProductParam();
		params.setCategoryId(EmptyUtils.isNotEmpty(categoryId) ? Integer.parseInt(categoryId) : null);
		params.setKeyword(keyWord);
		params.openPage((currentPage - 1) * rowPerPage, rowPerPage);
		int total = productService.queryProductCount(params);
		
		Pager pager = new Pager(total, rowPerPage, currentPage);
		pager.setUrl("/Product?action=queryProductList&categoryId="+(EmptyUtils.isNotEmpty(categoryId) ? categoryId : null) + "&keyWord=" + (EmptyUtils.isNotEmpty(keyWord) ? keyWord : null));
		List<ProductCategoryVo> productCategoryVoList = productCategoryService.queryAllProductCategory();
		List<Product> pList = productService.queryProductList(params);
		request.setAttribute("pList", pList);
		request.setAttribute("pager", pager);
		request.setAttribute("total", total);
		request.setAttribute("keyWord", keyWord);
		request.setAttribute("pcList", productCategoryVoList);
		return "/pre/product/productList";
		
	}
	
	public void init() throws ServletException {
		productService = new ProductServiceImpl();
		productCategoryService = new ProductCategoryServiceImpl();
	}


	@Override
	public Class getServletClass() {
		return ProductServlet.class;
	}
	
	
	/**
	 * 	查询商品的详情
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String queryProductDeatil(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String id = request.getParameter("id");
		Product product = productService.findById(id);
		List<ProductCategoryVo> productCategoryVoList = productCategoryService.queryAllProductCategory();
		request.setAttribute("product", product);
		request.setAttribute("pcList", productCategoryVoList);
		return "/pre/product/productDeatil";
		
	}

}
